package com.jia.together.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.together.entity.*;
import com.jia.together.entity.Blog;
import com.jia.together.mapper.BlogMapper;
import com.jia.together.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.together.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2021-02-20
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MemberService memberService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private MemberBlogService memberBlogService;

    @Override
    public Map<String, Object> selectPageList(String categoryId,Long pageNum, Long pageSize) {

        Page<Blog> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(categoryId)){
            wrapper.eq("category_id",categoryId);
        }
        wrapper.orderByDesc("gmt_create");
        Page<Blog> resourcePage = baseMapper.selectPage(page, wrapper);
        List<Blog> records = resourcePage.getRecords();
        long total = resourcePage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records", records);
        map.put("total", total);
        return map;

    }

    @Override
    public Map<String, Object> queryList(String key, String categoryId, Long pageNum, Long pageSize) {

        Page<Blog> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            wrapper.like("title",key).or().like("description",key);
        }
        if (!StringUtils.isEmpty(categoryId)){
            wrapper.eq("category_id",categoryId);
        }
        wrapper.orderByAsc("gmt_create");
        Page<Blog> blogPage = baseMapper.selectPage(page, wrapper);
        List<Blog> records = blogPage.getRecords();
        records.forEach(item->{
            Member member = memberService.getById(item.getMemberId());
            Category category = categoryService.getById(item.getCategoryId());
            System.out.println("member.getNickname"+member.getNickname());
            System.out.println("member.getAvatar()"+member.getAvatar());
            System.out.println("category.getTitle()"+category.getTitle());
            item.setMemberName(member.getNickname());
            item.setMemberAvatar(member.getAvatar());
            item.setCategoryName(category.getTitle());
        });
        long total = blogPage.getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return map;
    }

    @Override
    public Blog selectInfoById(String id) {
        Blog blog = baseMapper.selectById(id);
        Member member = memberService.getById(blog.getMemberId());
        Category category = categoryService.getById(blog.getCategoryId());
        blog.setMemberName(member.getNickname());
        blog.setMemberAvatar(member.getAvatar());
        blog.setCategoryName(category.getTitle());
        return blog;
    }

    @Override
    public void clickBlog(String blogId,String memberId) {
        // 点击添加文章记录逻辑 没有此功能

        // 点击查看数逻辑
        Object redisBlogViews = redisUtil.get(blogId + "_blog_views");
        if (redisBlogViews == null){
            Blog blog = baseMapper.selectById(blogId);
            redisUtil.set(blogId + "_blog_views",blog.getViews() + 1);
        }else {
            Integer view = ((Integer) redisBlogViews) + 1;
            redisUtil.set(blogId + "_blog_views",view);
        }
    }

    // 定时器 1小时执行一次
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void updateBlogViews() {
        List<Blog> blogs = baseMapper.selectList(null);
        blogs.forEach(blog -> {
            Integer redisBlogView = (Integer) redisUtil.get(blog.getId() + "_blog_views");
            blog.setViews(redisBlogView);
            baseMapper.updateById(blog);
        });
    }

    @Override
    public void collectBlog(String blogId, String memberId) {
        if (!StringUtils.isEmpty(blogId) && !StringUtils.isEmpty(memberId)){
            QueryWrapper<MemberBlog> wrapper = new QueryWrapper<>();
            wrapper.eq("collect_blog_id",blogId);
            wrapper.eq("member_id",memberId);
            MemberBlog one = memberBlogService.getOne(wrapper);
            if (one == null){
                MemberBlog memberBlog = new MemberBlog();
                memberBlog.setCollectBlogId(blogId);
                memberBlog.setMemberId(memberId);
                memberBlogService.save(memberBlog);
            }
        }
    }
}
